package com.jianzh5.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.jianzh5.common.DataPage;
import com.jianzh5.dao.BaseDaoI;
import com.jianzh5.entity.TResource;
import com.jianzh5.entity.TRole;
import com.jianzh5.common.DataTables;
import com.jianzh5.pojo.PResources;
import com.jianzh5.pojo.PRole;
import com.jianzh5.service.RoleServiceI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service("roleService")
public class RoleServiceImpl implements RoleServiceI {
	@Autowired
	private BaseDaoI<TRole> roleDao;
	@Autowired
	private BaseDaoI<TResource> resourceDao;
	
	public DataTables dataTables(String order, String sort, DataPage datapage) {
		String hql = "from TRole t";
		String totalHql = "select count(*) from TRole t";
		hql += " order by t."+sort+" "+ order;
		List<TRole> tlist = roleDao.find(hql);
		List<PRole> plist = Lists.newArrayList();
		for(TRole t : tlist){
			PRole p = new PRole();
			BeanUtils.copyProperties(t, p);
			plist.add(p);
		}
		long records = roleDao.count(totalHql);
		DataTables dt = new DataTables();
		dt.setDraw(datapage.getDraw());
		dt.setRecordsTotal(records);
		dt.setRecordsFiltered(records);
		dt.setData(plist);
		return dt;
	}

	public List<PRole> getRoleList() {
		String hql = "from TRole t order by t.seq asc";
		List<TRole> tlist = roleDao.find(hql);
		List<PRole> plist = Lists.newArrayList();
		for(TRole t : tlist){
			PRole p = new PRole();
			BeanUtils.copyProperties(t, p);
			plist.add(p);
		}
		return plist;
	}
	
	public List<PResources> getRoleTree() {
		String hql = "from TResource t order by t.seq asc";
		List<TResource> resList = resourceDao.find(hql);
		List<PResources> plist = Lists.newArrayList();
		for(TResource res : resList){
			PResources p = new PResources();
			BeanUtils.copyProperties(res, p);
			if(res.getTResource() != null){
				p.setPid(res.getTResource().getId());
			}
			plist.add(p);
		}
		System.out.println(plist.toString());
		return plist;
	}

	public void delRole(int roleId) {
		TRole t = roleDao.get(TRole.class, roleId);
		roleDao.delete(t);
	}

	public PRole getRole(int roleId) {
		TRole t = roleDao.get(TRole.class, roleId);
		PRole p = new PRole();
		BeanUtils.copyProperties(t, p);
		List<Integer> permissionList = Lists.newArrayList();
		Set<TResource> resSet = t.getTResources();
		if(resSet != null && !resSet.isEmpty()){
			for(TResource res : resSet){
				permissionList.add(res.getId());
			}
		}
		p.setPermissionList(permissionList);
		return p;
	}

	public void editRole(PRole prole) {
		TRole t = roleDao.get(TRole.class,prole.getId());
		BeanUtils.copyProperties(prole, t);
		Set<TResource> sets = Sets.newHashSet();
		if(StringUtils.isNotBlank(prole.getMenuIds())){
			for(String s : prole.getMenuIds().split(",")){
				TResource res = resourceDao.get(TResource.class, Integer.parseInt(s));
				sets.add(res);
			}
		}
		t.setTResources(sets);
	}

	public void addRole(PRole prole) {
		TRole t = new TRole();
		BeanUtils.copyProperties(prole, t);
		Set<TResource> sets = Sets.newHashSet();
		if(StringUtils.isNotBlank(prole.getMenuIds())){
			for(String str : prole.getMenuIds().split(",")){
				TResource res = resourceDao.get(TResource.class, Integer.parseInt(str));
				sets.add(res);
			}
		}
		t.setTResources(sets);
		roleDao.save(t);
	}

	public TRole getRoleByName(String rolename) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("rolename", rolename);
		String hql = "from TRole t where t.rolename = :rolename";
		TRole t = roleDao.get(hql, params);
		return t;
	}
	
}
